package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("车辆查询:VehicleQuery+{}", vehicleQuery);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        return list;

    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("车辆保存:VehicleSaveParam+{}", vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam, vehicle);
        VehicleVO vehicleVO =vehicleMapper.selectByLicense(vehicle.getLicense());
        if (vehicle.getId() == null){//新增
            if (vehicleVO != null){//如果车牌号已存在，说明即将要插入的车牌号重复
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setCreateTime(new Date());
            vehicle.setStatus("1");//空闲
            vehicle.setGeofenceBindStatus("0");//未绑定围栏
            vehicleMapper.insert(vehicle);
        }else {//更新
            //如果车牌号已存在，且当前传入的车牌号车辆id不是当前要更新的车辆id，说明即将要插入的车牌号重复
            if (vehicleVO != null && vehicleVO.getId() != vehicle.getId()){
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }

    }

    @Override
    public void deleteVehicle(Long vehicleId) {
        log.debug("车辆删除:vehicleId+{}", vehicleId);
        vehicleMapper.delete(vehicleId);
        log.debug("车辆删除完成");
    }

    @Override
    public void unbindVehicle(Long vehicleId) {
        log.debug("车辆解绑:vehicleId+{}", vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setGeofenceId(null);
        vehicle.setUpdateTime(new Date());
        /* 之前的update SQL要求围栏id不为null才会拼接更新条件
         * 但此处的围栏id就需要设置为null值,所以复用不了,我们可以写一个新的SQL*/
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public void bindVehicle(Long geofenceId, Long vehicleId) {
        log.debug("车辆绑定:geofenceId+{},vehicleId+{}", geofenceId, vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geofenceId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }


}
